<?php

namespace App\Validators\System;

use App\Validators\PassesValidator;
use Illuminate\Support\Facades\Input;
use Prettus\Validator\Contracts\ValidatorInterface;
use Prettus\Validator\LaravelValidator;

class SystemCodeValidator extends LaravelValidator{

    use PassesValidator;

    protected $rulesMessages=[
        'type.required' => '类型是必填的',
        'type_name.required' => '类型名称是必填的',
        'value.required'  => '值是必填的',
        'value_name.required'  => '值名称是必填的',
        'value.unique'   =>  '该类型已经存在了该值',
        ];

    protected $rules = [
        ValidatorInterface::RULE_CREATE => [
            'type' => 'required',
            'type_name' => 'required',
            'value' => 'required|unique:system_code,value,NULL,id,type,{type}',
            'value_name' => 'required',
        ],
        ValidatorInterface::RULE_UPDATE => [
            'type' => 'required',
            'type_name' => 'required',
            'value' => 'required|unique:system_code,value,{id},id,type,{type}',
            'value_name' => 'required',
        ],
   ];
    public function callBackGetRules(array $rules){
        $result=[];
        $value_change = ['{id}'=>Input::get('id'),'{type}'=>Input::get('type')];
        foreach ($rules as $key=>$value){
            if(is_string($value) && (str_contains($value,'{id}') || str_contains($value,'{type}') )){
                $result[$key]=strtr($value,$value_change);
            }else{
                $result[$key]=$value;
            }
        }
        return $result;
    }
}
